import wx
from configobj import ConfigObj

# 设置串口参数的对话框类

class UI_Com_Setting(wx.Dialog):

    def __init__(self, *args, **kwds):

        # 先处理输入参数，然后父类初始化
        if 'comSetting' in kwds:
            print('comSetting transport success ')
            self.comSetting = kwds.pop('comSetting')



        # 参数调入后初始化
        super(UI_Com_Setting, self).__init__(*args, **kwds)

        self.InitUI()
        self.SetSize((400, 400))
        self.SetTitle("串口设置")
        # self.ComSettingData = ComSettingData

    def InitUI(self):

        self.panel = wx.Panel(self)
        self.vbox = wx.BoxSizer(wx.VERTICAL)

        self.sb = wx.StaticBox(self.panel, label='多个串口工作')
        self.sbs = wx.StaticBoxSizer(self.sb, orient=wx.VERTICAL)
        print("ui com setting %s" %self.comSetting)
        #self.ComSettingData = {"1":"1;9600", "2":"2;9600", "3":"3;9600"}

        self.hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText1 = wx.StaticText(self.panel, label='一号串口' )
        self.textCtrl1   = wx.TextCtrl(self.panel, value="")
        if len(self.comSetting) >= 1 :       
            self.textCtrl1.SetValue(self.comSetting['1'])
        self.hbox1.Add(self.staticText1)
        self.hbox1.Add(self.textCtrl1, flag=wx.LEFT, border=15)
        self.sbs.Add(self.hbox1)

        self.hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText2 = wx.StaticText(self.panel, label='二号串口' )
        self.textCtrl2   = wx.TextCtrl(self.panel, value="")
        if len(self.comSetting) >= 2 :
            self.textCtrl2.SetValue(self.comSetting['2'])
        self.hbox2.Add(self.staticText2)
        self.hbox2.Add(self.textCtrl2,flag=wx.LEFT, border=15)
        self.sbs.Add(self.hbox2)

        self.hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        self.staticText3 = wx.StaticText(self.panel, label='三号串口' )
        self.textCtrl3  = wx.TextCtrl(self.panel, value="")
        if len(self.comSetting) >= 3 :
            self.textCtrl3.SetValue(self.comSetting['3'])
        self.hbox3.Add(self.staticText3)
        self.hbox3.Add(self.textCtrl3,flag=wx.LEFT, border=15)

        self.sbs.Add(self.hbox3)

        self.panel.SetSizer(self.sbs)

        self.hbox_0 = wx.BoxSizer(wx.HORIZONTAL)
        self.okButton = wx.Button(self, label=u'保存配置')
        self.closeButton = wx.Button(self, label='Close')
        self.hbox_0.Add(self.okButton)
        self.hbox_0.Add(self.closeButton, flag=wx.LEFT, border=5)

        self.vbox.Add(self.panel, proportion=1,
                flag=wx.ALL | wx.EXPAND, border=5)
        self.vbox.Add(self.hbox_0, flag=wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, border=10)

        self.SetSizer(self.vbox)

        self.okButton.Bind(wx.EVT_BUTTON, self.OnSave)
        self.closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
 
    # def LoadData(self,e):

    #     pass

    def saveData(self, e):

        pass   

    def OnClose(self, e):

        self.Destroy()

    def OnSave(self, e):
        success = True
        # 赋值字典，写入文件   
        conf_ini = "config.ini"
        config = ConfigObj(conf_ini)
        config['ComSetting']['1'] = self.textCtrl1.GetValue( )
        config['ComSetting']['2'] = self.textCtrl2.GetValue( )
        config['ComSetting']['3'] = self.textCtrl3.GetValue( )
        config.write()
        # 写入失败 赋值success false
        if success:
            self.EndModal(wx.ID_OK)

 
    def OnOK(self, events):
        success = True
        self.serial.port = self.ports[self.choice_port.GetSelection()]
        if self.show & SHOW_BAUDRATE:
            try:
                b = int(self.combo_box_baudrate.GetValue())
            except ValueError:
                with wx.MessageDialog(
                        self,
                        'Baudrate must be a numeric value',
                        'Value Error',
                        wx.OK | wx.ICON_ERROR) as dlg:
                    dlg.ShowModal()
                success = False
            else:
                self.serial.baudrate = b
        if self.show & SHOW_FORMAT:
            self.serial.bytesize = self.serial.BYTESIZES[self.choice_databits.GetSelection()]
            self.serial.stopbits = self.serial.STOPBITS[self.choice_stopbits.GetSelection()]
            self.serial.parity = self.serial.PARITIES[self.choice_parity.GetSelection()]
        if self.show & SHOW_FLOW:
            self.serial.rtscts = self.checkbox_rtscts.GetValue()
            self.serial.xonxoff = self.checkbox_xonxoff.GetValue()
        if self.show & SHOW_TIMEOUT:
            if self.checkbox_timeout.GetValue():
                try:
                    self.serial.timeout = float(self.text_ctrl_timeout.GetValue())
                except ValueError:
                    with wx.MessageDialog(
                            self,
                            'Timeout must be a numeric value',
                            'Value Error',
                            wx.OK | wx.ICON_ERROR) as dlg:
                        dlg.ShowModal()
                    success = False
            else:
                self.serial.timeout = None
        if success:
            self.EndModal(wx.ID_OK)


